package com.logic.landseaserver.service.bill.offrent;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.domain.BillDetail;
import com.logic.landseaserver.domain.Contract;
import com.logic.landseaserver.domain.MonthlyBill;
import com.logic.landseaserver.domain.PreOffrentBill;
import com.logic.landseaserver.domain.PreOffrentDetailBill;
import com.logic.landseaserver.service.IOffrentPreBill;

/**
 * Created by Junny on 2017/8/23.
 */
public abstract class AbstractOffrentPreBill implements IOffrentPreBill {

    private static final Logger LOG = LoggerFactory.getLogger(AbstractOffrentPreBill.class);

    public Integer addPreBill(Contract contract, boolean isNeedProcessAccount)throws LandseaException{
        int contractId = contract.getId();
        
        Integer preBillId = null;
        
        //判断是否存在有效的预结算单，如存在则删除，不存在则新增
        PreOffrentBill preBills = queryPreBill(contractId);
        if(preBills!=null){
            deletePreBill(preBills.getId());
            preBillId = createPreBillDetails(contract, isNeedProcessAccount);
        }else{
            preBillId = createPreBillDetails(contract, isNeedProcessAccount);
        }
        
        return preBillId;
    }

    public void deletePreBillByContractId(Integer contractId){
        //判断是否存在有效的预结算单，如存在则删除，不存在则新增
        PreOffrentBill preBills = queryPreBill(contractId);
        if(preBills!=null){
            deletePreBill(preBills.getId());
        }
    }

    /**
     * 根据contractId取消预结算单
     * @param contractId
     * @return
     */
    public boolean cancelPreBill(Integer contractId){
        //判断是否存在有效的预结算单，如存在则删除，不存在则新增
        PreOffrentBill preBills = queryPreBill(contractId);
        if(preBills!=null){
            return cancelPreBillById(preBills.getId());
        }else{
           return true;
        }
    }

    /**
     * 获取预结算单
     * @param contractId 合同编号
     * @return
     * @throws LandseaException
     */
    public PreOffrentBill queryPreBillByContractId(Integer contractId) throws LandseaException{
            return queryPreBill(contractId);
    }

    /**
     * 获取预结算单详细情况
     * @param preBillId 预结算单id
     * @param preBillId 查询账单类型
     * @return
     * @throws LandseaException
     */
    public List<PreOffrentDetailBill> queryPreBillDetailByPreId(Integer preBillId,String billType) throws LandseaException{
        return queryPreOffDetails(preBillId,billType);
    }

    /**
     * 获取上次已缴费记录
     * @param contractId
     * @return
     */
    public List<BillDetail> queryLastPayBill(int contractId)throws LandseaException{
        return queryLastPayBillDetail(contractId);
    }

    /**
     * 创建预结算单子项
     * @param contract
     * @throws LandseaException
     */
    protected abstract Integer createPreBillDetails(Contract contract, boolean isNeedProcessAccount)throws LandseaException;

    /**
     * 根据合同编号查询预结算单
     * @param contractId
     * @return
     */
    protected abstract PreOffrentBill queryPreBill(Integer contractId);

    /**
     * 根据preBillId删除预结算单
     * @param preBillId
     * @return
     */
    protected abstract void deletePreBill(Integer preBillId);


    /**
     * 根据preBillId删除预结算单
     * @param preBillId
     * @return
     */
    protected abstract boolean cancelPreBillById(Integer preBillId);

    /**
     * 获取上次已缴费记录
     * @param contractId
     * @return
     */
    protected abstract List<BillDetail> queryLastPayBillDetail(int contractId)throws LandseaException;

    /**
     * 计算交割单的用量
     * @param contract
     * @param billDetails
     * @return
     */
    protected abstract  List<PreOffrentDetailBill> checkFeeLists(Contract contract,List<BillDetail> billDetails,List<String> ckFees)throws LandseaException;

    /**
     * 获取未缴的其余费项
     * @param contract
     * @return
     */
    protected abstract List<MonthlyBill> queryMonthyNoPayBill(Contract contract,List<String> ckFees);


    /**
     * 获取预结算单详细情况
     * @param preBillId
     * @return
     */
    protected abstract List<PreOffrentDetailBill> queryPreOffDetails(Integer preBillId,String billType);


}
